<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

/**
 * Class LaravelEchoTest
 *
 * @see resources/test/laravel-echo/src/main.js - 有使用说明
 *
 * @package App\Events
 */
class LaravelEchoTest implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * 如果没有定义 broadcastWith 函数
     * 所有类的公有属性都会被序列化后传给前端
     *
     * @var array
     */
    public $broadcast_data;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->broadcast_data = ['some data send to front end'];
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('my-channel');
    }

    /**
     * 自定义的事件名
     *
     * 如果没写这个方法前端收到的事件名是 LaravelEchoTest
     *
     * The event's broadcast name.
     *
     * @return string
     */
//    public function broadcastAs()
//    {
//        return 'my-event';
//    }

    /**
     * 定义了此函数，会广播给前端此函数的返回值，类的公有属性不会返回前端
     *
     * @return array
     */
    public function broadcastWith()
    {
        return ['id' => 1];
    }
}
